package com.tencent.cloud.uikit.widget.edittext;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;

import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.uikit.R;

import java.util.regex.Pattern;

/**
 * @author vinsonhuang
 * @date 2018/6/26
 */
public class LineTwoEditText extends AppCompatEditText implements View.OnFocusChangeListener,
        TextWatcher {

    private static final int SINGLE_LINE = 1;

    private Paint mPaint;

    private boolean isError = false;

    /**
     * 输入长度，默认为0，表示不限制
     */
    private int mInputLength = 0;

    /**
     * 文本格式
     */
    private String mTextFormat = "";

    /**
     * 文本格式
     */
    private String mTextFormat2 = "";

    /**
     * 格式检查监听器
     */
    private OnCheckFormatListener mCheckFormatListener = null;

    /**
     * 文本改变监听器
     */
    private OnTextChangeListener mTextChangeListener = null;

    /**
     * @param context
     * @param attrs
     */
    public LineTwoEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(ResourcesUtils.getColor(R.color.text_color_primary));
        setBackground(null);
//        setTextColor(ResourcesUtils.getColor(R.color.text_color_primary));
//        setHintTextColor(ResourcesUtils.getColor(R.color.text_color_primary));

        setPadding(1, getPaddingTop(), getPaddingRight(), getPaddingBottom());
        setOnFocusChangeListener(this);
        setLines(SINGLE_LINE);
        addTextChangedListener(this);
    }

    public boolean isError() {
        return isError;
    }

    public void setError(boolean error) {
        isError = error;
        invalidate();
    }

    public int getInputLength() {
        return mInputLength;
    }

    public void setInputLength(int inputLength) {
        mInputLength = inputLength;
        if (mInputLength > 0) {
            setFilters(new InputFilter[]{new InputFilter.LengthFilter(mInputLength)});
        }
    }

    public String getTextFormat() {
        return mTextFormat;
    }

    public void setTextFormat(String textFormat) {
        mTextFormat = textFormat;
    }

    public String getmTextFormat2() {
        return mTextFormat2;
    }

    public void setmTextFormat2(String mTextFormat2) {
        this.mTextFormat2 = mTextFormat2;
    }

    public OnCheckFormatListener getCheckFormatListener() {
        return mCheckFormatListener;
    }


    public void setCheckFormatListener(OnCheckFormatListener checkFormatListener) {
        mCheckFormatListener = checkFormatListener;
    }

    public OnTextChangeListener getTextChangeListener() {
        return mTextChangeListener;
    }

    public void setTextChangeListener(OnTextChangeListener textChangeListener) {
        mTextChangeListener = textChangeListener;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int padB = getPaddingBottom();
        int padR = getPaddingRight();
//        if (isError) {
//            mPaint.setColor(Color.RED);
//        } else {
//            mPaint.setColor(Color.WHITE);
//        }
        canvas.drawLine(0, getHeight() - padB, getWidth() - padR, getHeight() - padB, mPaint);
    }

    /**
     * 失去焦点时，对输入进行校验
     *
     * @param v
     * @param hasFocus
     */
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (null == mCheckFormatListener) {
            return;
        }
        EditText editText = (EditText) v;
        String inputData = editText.getText().toString();
        if (!hasFocus) {
            checkInputData(inputData);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    /**
     * 输入文本改变后，对输入进行校验
     *
     * @param s
     */
    @Override
    public void afterTextChanged(Editable s) {
        if (null == mCheckFormatListener) {
            return;
        }
        String inputData = s.toString();
        if (TextUtils.isEmpty(inputData)) {
            return;
        }
        if (0 == mInputLength || inputData.length() == mInputLength) {
//            checkInputData(inputData);
        }
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (null != mTextChangeListener) {
            mTextChangeListener.onTextChanged(s, start, before, count);
        }
    }

    public interface OnTextChangeListener {
        void onTextChanged(CharSequence s, int start, int before, int count);
    }

    public interface OnCheckFormatListener {
        void onSuccess();

        void onFailed();
    }

    private void checkInputData(String inputData) {
        boolean isSuccess;
        if (TextUtils.isEmpty(inputData)) {
            isSuccess = false;
        } else if (mInputLength > 0 && inputData.length() != mInputLength) {
            isSuccess = false;
        } else if (TextUtils.isEmpty(mTextFormat)) {
            isSuccess = true;
        } else {
            isSuccess = (Pattern.compile(mTextFormat).matcher(inputData).matches()
                    || Pattern.compile(mTextFormat2).matcher(inputData).matches());
        }

        if (isSuccess) {
            mCheckFormatListener.onSuccess();
        } else {
            mCheckFormatListener.onFailed();
        }
        setError(!isSuccess);
    }


}
